//
//  YSCartItemFrame.m
//  Yueshi
//
//  Created by Northwan on 15/7/27.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSCartItemFrame.h"
#import "YSPresent.h"


@implementation YSCartItemFrame

-(void)setProduct:(YSCartProduct *)product{
    _product = product;
    
    self.iconFrame = CGRectMake(YSPublicXInset *5 , 15, ICON_HEIGHT, ICON_HEIGHT);
    
    CGFloat nameX = CGRectGetMaxX(self.iconFrame)+15;
    CGSize nameSize = [product.prodName sizeWithFont:SYS_FONT(NAME_FONT)constrainedToSize:CGSizeMake(SCREENW-CGRectGetMaxX(self.iconFrame)-30, [UIFont systemFontOfSize:NAME_FONT].lineHeight * 4)];
    self.nameFrame = (CGRect){{nameX, 15}, nameSize};
    
    CGSize colorSize = [product.prodBuyLink sizeWithFont:SYS_FONT(SUB_FONT)];
    self.colorFrame = CGRectMake(nameX, CGRectGetMaxY(self.nameFrame)+8, colorSize.width, colorSize.height);
    
    CGSize newPriceSize = [[NSString stringWithFormat:@"¥%.1f",product.prodNewPrice] sizeWithFont:SYS_FONT(NEWPRICE_FONT)];
    self.newPriceFrame = CGRectMake(nameX, CGRectGetMaxY(self.iconFrame)-NEWPRICE_FONT, newPriceSize.width, NEWPRICE_FONT);
    
    
    self.calcY = CGRectGetMaxY(self.iconFrame)- SCALEX(25);
    
    if(product.prodPromotion){
        if(product.prodPromotion.isPanicBuy){//买赠
            //抢购价格
            CGFloat promotionPriceX = CGRectGetMinX(self.iconFrame);
            CGFloat promotionPriceY = CGRectGetMaxY(self.iconFrame)+YSPublicXInset;
            
            NSString *formatPriceStr = nil;
            if(product.prodPromotion.whetherShipping){
                formatPriceStr = [NSString stringWithFormat:@"【限时包邮价】%.1f",product.prodPromotion.price];
            }else{
                formatPriceStr = [NSString stringWithFormat:@"【限时抢购价】%.1f",product.prodPromotion.price];
            }
            CGSize promotionPriceSize = [formatPriceStr sizeWithFont:SYS_FONT(NAME_FONT)];
            self.promotionPriceFrame = CGRectMake(promotionPriceX, promotionPriceY, promotionPriceSize.width, promotionPriceSize.height);
            
            //抢购时间
            NSString *promotionTime = [NSString stringWithFormat:@"仅剩%@",[TimeUtils getFormatRemainTime:[TimeUtils getRemainTime:product.prodPromotion.endTime]]];
            CGSize promotionTimeSize = [promotionTime sizeWithFont:SYS_FONT(NAME_FONT)];
            self.promotionTimeFrame = CGRectMake(SCREENW-promotionTimeSize.width-YSPublicXInset, CGRectGetMinY(self.promotionPriceFrame), promotionTimeSize.width, promotionTimeSize.height);
            self.cellHeight = CGRectGetMaxY(self.promotionTimeFrame)+YSPublicXInset;
                
        }else if(product.prodPromotion.isPresent){//买赠
             YSPresent *present = product.prodPromotion.presentList[0];
             NSString  *presentName = [NSString stringWithFormat:@"买就赠:%@",present.name];
            CGSize presentSize = [presentName sizeWithFont:SYS_FONT(NAME_FONT)];
            CGFloat presentPriceX = CGRectGetMinX(self.iconFrame);
            CGFloat presentPriceY = CGRectGetMaxY(self.iconFrame)+YSPublicXInset;
            self.presentFrame = CGRectMake(presentPriceX, presentPriceY, presentSize.width, presentSize.height);
            self.cellHeight = CGRectGetMaxY(self.presentFrame)+YSPublicXInset;
        }else{
             self.cellHeight = CGRectGetMaxY(self.iconFrame)+YSPublicXInset;
        }
    }else{
         self.cellHeight = CGRectGetMaxY(self.iconFrame)+YSPublicXInset;
    }
   
    self.statusFrame = CGRectMake(YSPublicXInset, (self.iconFrame.size.width+YSPublicXInset)/2, 20, 20);
    
    self.coverStatusFrame = CGRectMake(0, 0, YSPublicXInset*5, self.cellHeight);
    
    self.frame = CGRectMake(0, 0, SCREENW,self.cellHeight);
}


@end
